<?php
namespace addon\slide\validate;
use app\common\validate\ValidateBase;
use think\Db;
class SlideCat extends ValidateBase
{
    // 验证规则
    protected $rule = [
        'name'              => 'require',
        'sign'              => 'require|check_sign',
    ];

    // 验证提示
    protected $message = [
        'name.require'          => '分类名称必须',
        'sign.require'          => '广告标识必须'
    ];

    // 应用场景
    protected $scene = [
        'add'  =>  ['name','sign'],
        'edit'  => ['name','sign'],
    ];
    protected function check_sign($value){
        $id=input('id/d',0);
        // 编辑
        if($id){
            if(Db::name("SlideCat")->where("sign='$value' and id!=$id")->count()>0){
                return "该标识已经存在";
            }
        }else{
            if(Db::name("SlideCat")->where("sign='$value'")->count()>0){
                return "该标识已经存在";
            } 
        }
        return true;
    }
}